package main

import (
	"errors"
	"fmt"
	"net/http"
	"strconv"
	"sync"
	"time"
)

func SendData(i int) error {
	c := &http.Client{
		Timeout: 10 * time.Second,
	}
	url := "http://10.10.7.179:5702/sleep?taskid=" + strconv.Itoa(i)
	resp, err := c.Get(url)

	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return errors.New("fail")
	}

	return nil
}

func main() {
	fmt.Println("test")
	MaxSend := 2
	var beginWg sync.WaitGroup
	fmt.Println("starting work with MaxSend:", MaxSend)
	n, m := 0, 0
	j := 0
	f := func(i int) {
		for {
			// t1 := time.Now()
			taskid := i*10000000 + j
			// fmt.Println(taskid)
			err := SendData(taskid)
			j++
			if err != nil {
				fmt.Println(err)
				n += 1
			} else {
				m += 1
			}
		}
	}

	for i := 0; i < MaxSend; i++ {
		beginWg.Add(1)
	}
	fmt.Println("working...")
	for i := 1; i <= MaxSend; i++ {
		go f(i)
		beginWg.Done()
	}

	time.Sleep(30 * time.Second)
	// fmt.Println("Time:", 30, "s success:", m, "failure:", n)
	fmt.Println(MaxSend, m, n)
}
